package com.jackie.easyble.ui.adapter

import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattDescriptor
import android.bluetooth.BluetoothGattService
import android.content.Context
import android.graphics.Color
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.jackie.easyble.R
import com.jackie.easyble.bean.BluetoothGattCharacteristicWithBoolean
import com.jackie.easyble.bean.BluetoothGattServiceWithBoolean
import com.jackie.easyble.databinding.AdapterDeviceServiceItemBinding
import com.jackie.easyble.toastL
import com.sscl.baselibrary.utils.DefaultItemDecoration

class DeviceServiceRecyclerViewAdapter :
    BaseQuickAdapter<BluetoothGattServiceWithBoolean, DeviceServiceRecyclerViewAdapter.ViewHolder>() {

    interface OnCharacterOptionClickListener {

        fun onCharacterOption(characteristic: BluetoothGattCharacteristic)
    }

    interface OnDescriptorOptionClickListener {

        fun onDescriptorOption(descriptor: BluetoothGattDescriptor)
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 内部类
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    class ViewHolder(parent: ViewGroup) :
        DataBindingHolder<AdapterDeviceServiceItemBinding>(
            R.layout.adapter_device_service_item,
            parent
        )

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 构造方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    init {
        setOnItemClickListener { adapter, view, position ->
            if (items.isEmpty()) {
                context.toastL(
                    R.string.no_characteristic
                )
                return@setOnItemClickListener
            }
            val item = adapter.getItem(position) ?: return@setOnItemClickListener
            item.expand = !item.expand
            notifyItemChanged(position)
        }
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 属性声明
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /* * * * * * * * * * * * * * * * * * * 可空属性 * * * * * * * * * * * * * * * * * * */

    private var onCharacterOptionClickListener: OnCharacterOptionClickListener? = null
    private var onDescriptorOptionClickListener: OnDescriptorOptionClickListener? = null

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 实现方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * 实现此方法，并使用 [holder] 完成 item 视图的操作
     *
     * @param holder A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    override fun onBindViewHolder(
        holder: ViewHolder,
        position: Int,
        item: BluetoothGattServiceWithBoolean?
    ) {
        holder.binding.item = item
        setCharacteristicRvData(
            holder.binding.characteristicRv,
            item?.bluetoothGattService ?: return
        )
    }


    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        return ViewHolder(parent)
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 公开方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    fun setOnCharacterOptionClickListener(listener: OnCharacterOptionClickListener?) {
        onCharacterOptionClickListener = listener
    }

    fun setOnDescriptorOptionClickListener(listener: OnDescriptorOptionClickListener?) {
        onDescriptorOptionClickListener = listener
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 私有方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    private fun setCharacteristicRvData(
        recyclerView: RecyclerView,
        bluetoothGattService: BluetoothGattService
    ) {
        val cache = recyclerView.adapter
        val deviceCharacteristicRecyclerViewAdapter = if (cache == null) {
            recyclerView.layoutManager = LinearLayoutManager(context)
            recyclerView.addItemDecoration(DefaultItemDecoration.newLine(Color.GRAY))
            val cacheAdapter = DeviceCharacteristicRecyclerViewAdapter()
            recyclerView.adapter = cacheAdapter
            cacheAdapter
        } else {
            cache as DeviceCharacteristicRecyclerViewAdapter
        }
        while (true) {
            if (deviceCharacteristicRecyclerViewAdapter.items.isEmpty()) {
                break
            }
            deviceCharacteristicRecyclerViewAdapter.removeAt(0)
        }
        val characteristics = bluetoothGattService.characteristics ?: return
        for (characteristic in characteristics) {
            deviceCharacteristicRecyclerViewAdapter.add(BluetoothGattCharacteristicWithBoolean(false, characteristic))
        }
        deviceCharacteristicRecyclerViewAdapter.setOnItemLongClickListener(OnItemLongClickListener { adapter, view, position ->
            val characteristicWithBoolean =
                adapter.getItem(position) ?: return@OnItemLongClickListener true
            onCharacterOptionClickListener?.onCharacterOption(characteristicWithBoolean.bluetoothGattCharacteristic)
            return@OnItemLongClickListener true
        })
        deviceCharacteristicRecyclerViewAdapter.setOnDescriptorOptionClickListener(object : OnDescriptorOptionClickListener {
            override fun onDescriptorOption(descriptor: BluetoothGattDescriptor) {
                onDescriptorOptionClickListener?.onDescriptorOption(descriptor)
            }
        })
    }
}